﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.Validator
{
  public partial class Overview : SpreadDemoPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (IsPostBack)
      {
        return;
      }

      InitSpread(FpSpread1);
      InitSpreadStyles(FpSpread1.Sheets[0]);
      this.FpSpread1.UseClipboard = false;
    }

    private void InitSpread(FarPoint.Web.Spread.FpSpread spread)
    {
      spread.CssClass = "spreadStyle";
      spread.UseClipboard = false;
    }

    private void InitSpreadStyles(FarPoint.Web.Spread.SheetView sheet)
    {
      sheet.SelectionBackColor = System.Drawing.Color.Yellow;
      sheet.ColumnCount = 3;
      sheet.RowCount = 1;

      sheet.ColumnHeader.Cells[0, 0].Value = "校验单元格数据";
      sheet.ColumnHeader.Cells[0, 1].Value = "操作步骤";
      sheet.ColumnHeader.Cells[0, 2].Value = "输入列";
      
      sheet.Columns[0].Width = 170;
      sheet.Columns[1].Width = 270;
      sheet.Columns[2].Width = 100;

      sheet.Rows[0].Height = 70;

      RequiredFieldValidator requiredFieldValidator = new RequiredFieldValidator();
      requiredFieldValidator.ErrorMessage = "单元格不能为空。";

      RegularExpressionValidator regularExpressionValidator = new RegularExpressionValidator();
      regularExpressionValidator.ValidationExpression = "^[0-9]+$";
      regularExpressionValidator.ErrorMessage = "请输入[0-9]整形数据。";

      RangeValidator rangeValidator = new RangeValidator();
      rangeValidator.Type = ValidationDataType.Integer;
      rangeValidator.MinimumValue = "0";
      rangeValidator.MaximumValue = "9";
      rangeValidator.ErrorMessage = "请输入[0-9]整形数据。";

      FarPoint.Web.Spread.GeneralCellType labelCellType = new FarPoint.Web.Spread.GeneralCellType();
      labelCellType.Multiline = true;

      FarPoint.Web.Spread.GeneralCellType generalCellType = new FarPoint.Web.Spread.GeneralCellType();
      generalCellType.Validators.AddRange(new BaseValidator[]{requiredFieldValidator, 
                                                              regularExpressionValidator, 
                                                              rangeValidator});

      sheet.Cells[0, 0].CanFocus = false;
      sheet.Cells[0, 0].Locked = true;
      sheet.Cells[0, 1].Locked = true;
      sheet.Cells[0, 1].CanFocus = false;

      sheet.Cells[0, 0].CellType = labelCellType;
      sheet.Cells[0, 1].CellType = labelCellType;
      sheet.Cells[0, 2].CellType = generalCellType;

      sheet.Cells[0, 0].Text =
@"1. RequiredFieldValidator
2. RegularExpressionValidator
3. RangeValidator";

      sheet.Cells[0, 1].Text =
@"1. 清除文本并输入，点击回车查看效果。
2. 输入非数值文本，点击回车查看效果。
3. 输入 [0, 9] 范围外的数值，点击回车查看效果。
4. 输入数值，点击回车查看效果";

      
      sheet.Cells[0, 2].Value = "0";
      sheet.Cells[0, 2].Font.Bold = true;
      sheet.Cells[0, 2].Font.Size = FontUnit.Large;
      sheet.Cells[0, 2].HorizontalAlign = HorizontalAlign.Center;
      sheet.Cells[0, 2].VerticalAlign = VerticalAlign.Middle;
    }
  }
}